$(document).ready(function(){
	
	$('#putRequestNumber').focus();
	
	$("#putRequestNumber").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});
});

//เมื่อกดปุ่ม Enter
function Enter(e) {
	if (e.keyCode == 13) {
		searchRequestNumber();
	  return false;  
	}
};

//Function ค้นหา เลขที่ใบแจ้งราคา
function searchRequestNumber() {
	var requestNumber = $('#putRequestNumber').val();
	if (requestNumber == "") {
		$('#nullRequestNemberModal').modal();
	} else {
		$.ajax({           	
    		url : "CS52000Srvl",
    		type : "POST",
    		data : {methodType:"getData", requestNumber:requestNumber},
    		async : false ,
    		success : function(data) {
    			var newdata = jQuery.parseJSON(data);
    			if (newdata.message == "resultNull"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลใบแจ้งราคานี้");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "status1"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลใบแจ้งราคานี้ เนื่องจาก ไม่มีการคิดเงินในใบแจ้งราคานี้");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "status2"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลใบแจ้งราคานี้ เนื่องจาก ไม่สามารถรับเงินซ้ำได้");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "status3"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลใบแจ้งราคานี้ เนื่องจาก มีการปิดใบแจ้งราคาไปแล้ว");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "status4"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลใบแจ้งราคานี้ เนื่องจาก มีการยกเลิกใบแจ้งราคาไปแล้ว");
    				$('#failResultModal').modal();
    			} else {
    				var homeNumber = "";
    				var address = "";
    				var city = "";
    				var district = "";
    				var province = "";
    				var postCode = "";
    				var telephone = "";
    				$("#requestNumber").text(newdata.requestNumber);
    				if (newdata.meterSerialNumber == "null") {
    					$("#meterSerialNumber").text("");
    				} else {
    					$("#meterSerialNumber").text(newdata.meterSerialNumber);
    				}
        			if (newdata.account == "null"){
        				$("#account").text("");
        			} else {
        				$("#account").text(newdata.account);
        			}
        			$("#fullName").text(newdata.fullName);
        			if (newdata.homeNumber == "null") {
        				homeNumber = "";
        			}else {
        				homeNumber = newdata.homeNumber;
        			}
        			if (newdata.address == "null") {
        				address = "";
        			} else {
        				address = newdata.address;
        			}
        			if (newdata.city == null) {
        				city = "";
        			}else {
        				city = "ต. " + newdata.city;
        			}
        			if (newdata.district == null) {
        				district = "";
        			}else {
        				district = "อ. " + newdata.district;
        			}
        			if (newdata.province == null) {
        				province = "";
        			} else {
        				province = "จ. " + newdata.province;
        			}
        			if (newdata.postCode == "null") {
        				postCode = "";
        			} else {
        				postCode = newdata.postCode;
        			}
        			if (newdata.telephone == "null") {
        				telephone = "";
        			} else {
        				telephone = newdata.telephone;
        			}
        			$("#address").html(homeNumber + " " + address + " " + city + " " + district + " " + province + " " + postCode + " " + telephone);
        			$("#insertDate").text(newdata.insertDate);
        			$("#requestType").text(newdata.requestType);
        			$("#listAmount").text(newdata.description.length);
        			var totalAmount = 0.00;
        			var netAmount = 0.00;
        			var vatAmount = 0.00;
        			var html = "";
        			for (var i = 0; i < newdata.description.length; i++) {
        				html += "<tr class=\"odd\">";
        				
        				html += "<td>";
        				html +=  i + 1;
        				html += "</td>";
        				
        				html += "<td style=\"text-align:left;\">";
        				html +=  newdata.description[i];
        				html += "</td>";
        				
        				html += "<td style=\"text-align:right;\">";
        				html +=  format(newdata.receiveAmount[i], 2);
        				html += "</td>";
        				
        				html += "<td style=\"text-align:right;\">";
        				html +=  format(newdata.vatAmount[i], 2);
        				html += "</td>";
        				
        				html += "<td style=\"text-align:right;\" class=\"highlight-blue\">";
        				html += format(newdata.totalAmount[i], 2);
        				html += "</td>";
        				
        				html += "</tr>";
        				totalAmount += newdata.totalAmount[i];
        				netAmount += newdata.receiveAmount[i];
        				vatAmount += newdata.vatAmount[i];
        				$("table#tabelRequestQoute > tbody ").html(html);
        			}
        			$("#sum").text(format(parseFloat(totalAmount), 2));
        			$("#totalAll").text(format(parseFloat(totalAmount), 2));
        			$("#netAmount").val(format(parseFloat(netAmount), 2));
        			$("#vatAmount").val(format(parseFloat(vatAmount), 2));
        			$("#grossAmount").val(format(parseFloat(totalAmount), 2));
        			$("#inputNamePay").val("");
        			$("#inputAddressPay").val("");
    			}
    		}
		});	
	}
}

//Function ยืนยันการรับเงินค่าอื่นๆ
function confirm(){
	var requestNumber = $("#requestNumber").text();
	var namePay = $("#inputNamePay").val();
	var addressPay = $("#inputAddressPay").val();
	if (requestNumber == "") {
		$('#checkResultModal').modal();
	} else if (namePay == "") {
		$('#contentCheckComment').text("กรุณากรอกข้อมูล ชื่อ - สกุล ผู้ชำระเงิน");
		$('#checkCommentModal').modal();
	} else if (addressPay == "") {
		$('#contentCheckComment').text("กรุณากรอกข้อมูล ที่อยู่ผู้ชำระเงิน");
		$('#checkCommentModal').modal();
	} else {
		$('#confirmCancelModal').modal();
	}
}

//Function บันทึกการรับเงินค่าอื่นๆ
function save(){
	var requestNumber = $("#requestNumber").text();
	var account = $("#account").text();
	var namePay = $("#inputNamePay").val();
	var addressPay = $("#inputAddressPay").val();
	var netAmount = $("#netAmount").val();
	var vatAmount = $("#vatAmount").val();
	var grossAmount = $("#grossAmount").val();
	$.ajax({           	
		url : "CS52000Srvl",
		type : "POST",
		data : {methodType: "saveData", 
			requestNumber:requestNumber,
			account:account,
			namePay:namePay,
			addressPay:addressPay,
			netAmount:netAmount,
			vatAmount:vatAmount,
			grossAmount:grossAmount
		},
		async : false ,
		success : function(data) {
			var newdata = jQuery.parseJSON(data);
			if (newdata.message == "success") {
				$('#confirmCancelModal').modal('hide');
				$('#successModal').modal();
				$('#inputNamePay').val("");
				$('#inputAddressPay').val("");
				$('#putSerialNumber').val("");
			} else {
				$('#confirmCancelModal').modal('hide');
				$('#failModal').modal();
			}
		} 
	});
}

//Function เคลียร์หน้าจอ
function clearScreen(){
	$("#requestNumber").text("");
	$("#meterSerialNumber").text("");
	$("#account").text("");
	$("#insertDate").text("");
	$("#fullName").text("");
	$("#requestType").text("");
	$("#address").text("");
	$("#inputNamePay").val("");
	$("#inputAddressPay").val("");
	$("#sum").text("");
	$("#listAmount").text("");
	$("#totalAll").text("");
	$("table#tabelRequestQoute > tbody ").html("");
}

//รูปแบบ ตัวเลข
function format(num, fix) {
    var p = num.toFixed(fix).split(".");
    return p[0].split("").reduceRight(function(acc, num, i, orig) {
        if ("-" === num && 0 === i) {
            return num + acc;
        }
        var pos = orig.length - i - 1
        return  num + (pos && !(pos % 3) ? "," : "") + acc;
    }, "") + (p[1] ? "." + p[1] : "");
}

//Function ปิด Popup ค้นหา
function getFocus(){
	$(".close").trigger('click');
	$('#putRequestNumber').focus();
	clearScreen();
}

//Function ปิด Popup สำเร็จ
function getClose(){
	location.reload();
}

//Function ปิด Popup ยืนยัน
function cancel(){
	$(".close").trigger('click');
	var namePay = $("#inputNamePay").val();
	var addressPay = $("#inputAddressPay").val();
	if (namePay == "") {
		$('#inputNamePay').focus();
	} else if (addressPay == "") {
		$('#inputAddressPay').focus();
	} else {
		$('#putRequestNumber').focus();
	}
}